import axios from "axios";
import "nprogress/nprogress.css";
import {
  baseURL
} from "./tools";
import {
  getToken
} from "../api/auth";
import NProgress from "nprogress";
const instance = axios.create({
  baseURL: baseURL,
  timeout: 3000,
});

//请求拦截
instance.interceptors.request.use(
  function (config) {
    NProgress.start()

    if (getToken()) {
      //统一设置请求头
      config.headers.authorization = "Bearer " + getToken();
    }
    // Do something before request is sent
    return config;
  },
  function (error) {
    // Do something with request error
    return Promise.reject(error);
  }
);
//响应拦截
// Add a response interceptor
instance.interceptors.response.use(
  function (response) {
    NProgress.done()
    return response.data;
  },
  function (error) {


    return Promise.reject(error);
  }
);
//封装get请求
export const get = (url, params) => instance.get(url, {
  params: params
});
//封装post请求
export const post = (url, data) => instance.post(url, data);
//put请求
export const put = (url, data) => instance.put(url, data);

/**
 * delete
 * @param {*} url   地址
 * @returns
 */
export const del = (url) => instance.delete(url);

export default instance;